/**
 * 复制文件
 */
import common from '../../tnxet-common.js';
import util from '../main-util.js';
import fs from 'fs';

export default {
    name: common.event.file.copy,
    listen(sourceFilePath, targetFilePath, force) {
        return new Promise((resolve, reject) => {
            if (!sourceFilePath) {
                reject('Invalid sourceFilePath: ' + sourceFilePath);
            }
            if (!targetFilePath) {
                reject('Invalid targetFilePath: ' + targetFilePath);
            }
            sourceFilePath = util.file.getAbsolutePathBasedOnApp(sourceFilePath);
            targetFilePath = util.file.getAbsolutePathBasedOnApp(targetFilePath);
            fs.cp(sourceFilePath, targetFilePath, {force: !!force}, error => {
                if (error) {
                    reject(error);
                } else {
                    resolve();
                }
            });
        });
    },
}
